﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace td.Core.DiscoverAddins
{
    public partial class AutoRunList : DevExpress.XtraEditors.XtraUserControl
    {
        td.Core.Xml.AddIn addin;
        public AutoRunList(td.Core.Xml.AddIn addin)
        {
            InitializeComponent();

            this.addin = addin;
            this.listBoxControl1.SelectedIndexChanged += new EventHandler(listBoxControl1_SelectedIndexChanged);
            this.UpDateListInfo();
        }
        void UpDateListInfo()
        {
            this.listBoxControl1.Items.Clear();
            for (int i = 0; i < addin.AutoRun.Count; i++)
            {
                this.listBoxControl1.Items.Add(addin.AutoRun[i].Name);
            }
        }

        void listBoxControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetValue();
        }

        private void SetValue()
        {
            if (this.listBoxControl1.Items.Count <= 0) return;
            var t = addin.AutoRun[this.listBoxControl1.SelectedIndex];
            this.tbCaption.Text = t.Name;
            this.tbClass.Text = t.Class;
           
            this.tbToolTip.Text = t.Description;
          
        }
        //新建按钮
        private void simpleButton4_Click(object sender, EventArgs e)
        {
            ClearInfo();
        }

        private void ClearInfo()
        {
            this.tbCaption.Text = "";
            this.tbClass.Text = "";         
            this.tbToolTip.Text = "";
          
        }

        private void sbSave_Click(object sender, EventArgs e)
        {
            addin.AutoRun[this.listBoxControl1.SelectedIndex] = SaveValue();
            this.UpDateListInfo();
        }

        private td.Core.Xml.AutoStartXml SaveValue()
        {
            td.Core.Xml.AutoStartXml t = new Xml.AutoStartXml();
            t.Name = this.tbCaption.Text;
            t.Class = this.tbClass.Text;
            t.Description= this.tbToolTip.Text;

            return t;
        }

        private void sbNew_Click(object sender, EventArgs e)
        {
            addin.AutoRun.Add(SaveValue());            
            UpDateListInfo();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
           
            Reflection r = new Reflection(addin);
           
          
            if (r.ShowDialog() == DialogResult.OK)
            {
                this.tbClass.Text = r.Result;
            }

        }
        //删除
        private void sbdel_Click(object sender, EventArgs e)
        {
            if (this.listBoxControl1.SelectedItem != null)
            {
                addin.AutoRun.RemoveAt(this.listBoxControl1.SelectedIndex);
                UpDateListInfo();
            }
        }
    }
}
